package com.zlx.xbhy.controller;

import com.zlx.xbhy.entity.Article;
import com.zlx.xbhy.entity.Favorite;
import com.zlx.xbhy.entity.Result;
import com.zlx.xbhy.entity.User;
import com.zlx.xbhy.service.ArticleService;
import com.zlx.xbhy.service.FavoriteService;
import com.zlx.xbhy.service.UserService;
import com.zlx.xbhy.webSocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author 周罗校
 * @Description
 * @ClassName ArticleController
 * @date 2020.11.18 15:18:25
 * 该版权归周罗校所有，最终解释权以官方为准
 */

@RestController()
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService service;
    @Autowired
    private FavoriteService favoriteService;


    //分页
    @GetMapping("selectPage/{start}/{pageSize}")
    public Result selectPage(String title, @PathVariable Integer start,@PathVariable Integer pageSize){
        return new Result(service.selectPage(title,start,pageSize));
    }
    //获取收藏次数
    @GetMapping("getFavoriteCount/{aId}")
    public Result getFavoriteCount(@PathVariable Long aId){
        return new Result(favoriteService.getFavoriteCount(aId));
    }
    //浏览次数统计
    @PostMapping("addBrowseCount/{aid}/{browseCount}")
    public Result addBrowseCount(@PathVariable Long aid,@PathVariable Long browseCount){
        Article article = new Article();
        article.setId(aid);
        article.setBrowseCount(browseCount+1);
        service.updateByPrimaryKeySelective(article);
        return new Result(service.selectByPrimaryKey(aid));
    }
    @GetMapping("/isFavorite/{uid}/{aid}")
    public Result isFavorite(@PathVariable Long uid,@PathVariable Long aid){
        Favorite favorite = new Favorite();
        favorite.setaId(aid);
        favorite.setuId(uid);
        return new Result(favoriteService.selectCount(favorite));
    }
    @PostMapping("/toFavorite")
    public Result toFavorite(@RequestBody Favorite favorite, HttpSession session){
//        System.out.println(favorite.getaId()+""+favorite.getuId());
        favoriteService.favoriteUser(favorite);
        Article article = service.selectByPrimaryKey(favorite.getaId());
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser!=null&&article!=null){
            WebSocketServer.sendMsg(article.getUserId(),loginUser.getRealName()+"收藏了你的文章");
        }
        return new Result(true,"收藏成功！",null);
    }
    @PostMapping("/cancelFavorite")
    public Result cancelFavorite(@RequestBody Favorite favorite){
        favoriteService.cancelFavorite(favorite);
        return new Result(true,"取消收藏",null);
    }
    @PostMapping("addArticle")
    public Result addArticle(@RequestBody Article article){
        //设置文章发布时间
        article.setPublishDate(new Date());
        return new Result(true,"发布文章成功！",service.insert(article));
    }
    @GetMapping("selectFavoritePage/{start}/{pageSize}")
    public Result selectFavoritePage(@RequestParam("uId") Long uId,@RequestParam("title") String title,@PathVariable Integer start,@PathVariable Integer pageSize){
        return new  Result(service.selectFavoritePage(title,uId,start,pageSize));
    }
}
